package com.ideal.manage.esms.service.project;

import com.ideal.manage.esms.bean.project.ArithmeticDeployManage;
import com.ideal.manage.esms.bean.project.UserInfoDTO;
import com.ideal.manage.esms.bean.sys.Menu;
import com.ideal.manage.esms.bean.sys.Role;
import com.ideal.manage.esms.bean.sys.User;
import com.ideal.manage.esms.repository.framework.MySpecification;
import com.ideal.manage.esms.repository.framework.SpecificationOperator;
import com.ideal.manage.esms.repository.programme.AccountManageRepository;
import com.ideal.manage.esms.repository.programme.ArithmeticDeployManageRepository;
import com.ideal.manage.esms.repository.programme.ArithmeticManageRepository;
import com.ideal.manage.esms.service.system.RoleService;
import com.ideal.manage.esms.service.system.UserService;
import com.ideal.manage.esms.util.HttpRequests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by 83730 on 2018/4/24.
 */
@Service
public class AccountManageService {

    @Autowired
    private AccountManageRepository accountManageRepository;
    @Resource
    private UserService sysUserService;
    @Autowired
    RoleService roleService;
    /**
     * 分页 根据创建时间排序
     *
     * @param pageNum
     * @return
     */
//    @Cacheable(value = "arithmeticManagePage")
    public Page<UserInfoDTO> findAllManages(int pageNum, HttpServletRequest request) {
        List<SpecificationOperator> operators = HttpRequests.getParametersStartingWith(request, "Q_");
        Sort sort = new Sort(Sort.Direction.DESC, "timeKey");
        SpecificationOperator operator = new SpecificationOperator("del", "0", "EQ");
        operators.add(operator);
        MySpecification<UserInfoDTO> mySpecifications = new MySpecification<>(operators);
        Pageable pageable = new PageRequest(pageNum, 15, sort);

        Page<UserInfoDTO> page = accountManageRepository.findAll(mySpecifications, pageable);
        return page;
    }
    public UserInfoDTO findAllById(Integer id) {
        List<String> idList = new LinkedList<>();
        idList.add(String.valueOf(id));
        List<UserInfoDTO> list = accountManageRepository.findAllById(idList);
        if(list.size()>0) {
            System.out.println("list:"+list.get(0).getNickName()+list.get(0).getCustomerName()+list.get(0).getTimeKey());
            return list.get(0);
        } else {
            return null;
        }
    }
//    @CacheEvict(value = "arithmeticManagePage", allEntries = true)
    public void saveUserInfo(UserInfoDTO userInfoDTO) throws IOException {
//        MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(date));
        userInfoDTO.setTimeKey(sdf.format(date));
        userInfoDTO.setCustomerName(userInfoDTO.getNickName());
        if(userInfoDTO.getDel()==null){
            userInfoDTO.setDel(0);
        }
        saveSysUser(userInfoDTO);
        accountManageRepository.save(userInfoDTO);
    }
    private void saveSysUser(UserInfoDTO userInfoDTO){
        if(userInfoDTO==null||userInfoDTO.getAccount()==null){
            return;
        }
        UserInfoDTO oldUserInfoDto = null;
        if(userInfoDTO.getId()!=null){
            oldUserInfoDto = accountManageRepository.findById(userInfoDTO.getId());
        }
        String userLoginName = userInfoDTO.getAccount();
        String menu = userInfoDTO.getMenu();
        User sysOldUser = sysUserService.findByLoginName(oldUserInfoDto==null ? userLoginName: oldUserInfoDto.getAccount());
//        sysUserService.findByParameterName();
        if(menu==null||menu.indexOf("后台管理")==-1||userInfoDTO.getDel()==1){
            //删除后台管理权限
            if(sysOldUser!=null){
                sysOldUser.setDelFlag(1L);
                sysOldUser.setLoginName(userLoginName);
                sysUserService.saveUser(sysOldUser);
            }
        }else if(menu!=null && menu.indexOf("后台管理")!=-1){
            //增加后台管理权限
            Role role = roleService.findByName("admin");
            if(sysOldUser==null){
                sysOldUser = new User();
                sysOldUser.setCreateTime(new Date());
            }
            sysOldUser.setLoginName(userLoginName);
            sysOldUser.setDelFlag(0L);
            sysOldUser.setPassword(userInfoDTO.getPass());
            sysOldUser.setName(userLoginName);
            sysOldUser.setRole(role);
            sysOldUser.setJobTitle("后台管理");
            sysUserService.saveUser(sysOldUser);
        }
    }

    /**
     * 分页 根据创建时间排序
     *
     * @param ids
     * @return
     */
    public void delUserInfo(String [] ids) {
        List<UserInfoDTO> menus = accountManageRepository.findAll(Arrays.asList(ids));
        for(UserInfoDTO menu : menus){
            menu.setDel(1);
        }
        accountManageRepository.save(menus);
    }

}
